package fudan.se.project.service.infrastructure.func.conference.action.author;

import fudan.se.project.controller.request.PaperSubmitRequest;
import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.Conference;
import fudan.se.project.domain.conference.Topic;
import fudan.se.project.domain.conference.production.Paper;
import fudan.se.project.domain.conference.production.Writer;
import fudan.se.project.domain.conference.role.Author;
import fudan.se.project.service.infrastructure.entity.conference.ConferenceService;
import fudan.se.project.service.infrastructure.entity.conference.TopicService;
import fudan.se.project.service.infrastructure.entity.conference.production.PaperService;
import fudan.se.project.service.infrastructure.entity.conference.production.WriterService;
import fudan.se.project.service.infrastructure.entity.conference.role.AuthorService;
import fudan.se.project.util.user.UserDetailsUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedList;
import java.util.List;

@Service
public class PaperSubmitService {
    private TopicService topicService;
    private PaperService paperService;
    private ConferenceService conferenceService;
    private WriterService writerService;
    private UserDetailsUtil userDetailsUtil;
    private AuthorService authorService;

    public PaperSubmitService(TopicService topicService,
                              PaperService paperService,
                              ConferenceService conferenceService,
                              WriterService writerService, UserDetailsUtil userDetailsUtil,
                              AuthorService authorService
    ) {
        this.topicService = topicService;
        this.paperService = paperService;
        this.conferenceService = conferenceService;
        this.writerService = writerService;
        this.userDetailsUtil = userDetailsUtil;
        this.authorService = authorService;
    }


    public boolean process(PaperSubmitRequest request, MultipartFile file) {

        List<Long> topicsIdList = request.getTopicIds();
        List<Topic> topicList = new LinkedList<>();
        for (Long id : topicsIdList) {
            Topic topic;
            if ((topic = topicService.findById(id)) == null) {
                return false;
            } else {
                topicList.add(topic);
            }
        }

        Paper paper = paperService.save(new Paper());

        if (!paperService.store(request.getId(), paper.getId(), file)) {
            paperService.deletePaperEntity(paper);
            return false;
        } else {
            User currentUser = userDetailsUtil.currentUser();
            Conference conference = conferenceService.findById(request.getId());

            Author author = authorService.findByUserAndConference(currentUser, conference);
            if (author == null) {
                author = new Author(currentUser, conference);
                author = authorService.save(author);
            }
            paper.setTitleText(request.getTitleText());
            paper.setAbstractText(request.getAbstractText());
            paper.setOriginalFilename(file.getOriginalFilename());
            paper.setAuthor(author);
            paper.setConference(conference);
            paper.addTopics(topicList);

            paper = paperService.save(paper);

            int index = 0;
            List<Writer> writers = request.getWriters();
            for (Writer writer : writers) {
                writer.setPaper(paper);
                writer.setIndex(index++);
                writerService.save(writer);
            }

            return true;

        }
    }

}
